/**获取cookie中的对象*/
function getEntity(name){
	var entity;
	var str;
	//由于 容器不一样，cookie中可能带有引号，需要把引号去掉
	str=getCookie(name);
	if(str!=null&&str!='""'){
		//把cookie里的信息转换为用户会员对象
		//由于 容器不一样，cookie中可能带有引号，需要把引号去掉
		str=str.replace(/\"/g, "");
		str=decodeURI(str);
		str=decodeURIComponent(str);
		entity=toJson(str);
		return entity
	}else{
		return null;
	}
}
/**根据cookie的名字获取cookie里的信息*/
function getCookie(name){
	var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
	if(arr=document.cookie.match(reg))
	return unescape(arr[2]);
	else
	return null;
}
/**解析json字符串*/
function toJson(str){
  var json = eval('(' + str + ')');
  return json;
 }
/**获取url中的参数*/
function GetQueryString(name) {  
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");  
    var r = window.location.search.substr(1).match(reg);  //获取url中"?"符后的字符串并正则匹配
    var context = "";  
    if (r != null)  
         context = r[2];  
    reg = null;  
    r = null;  
    return context == null || context == "" || context == "undefined" ? "" : context;  
}
//定时器
function Pause(obj, iMinSecond) {
	if (window.eventList == null)
		window.eventList = new Array();
	var ind = -1;
	for (var i = 0; i < window.eventList.length; i++) {
		if (window.eventList[i] == null) {
			window.eventList[i] = obj;
			ind = i;
			break;
		}
	}

	if (ind == -1) {
		ind = window.eventList.length;
		window.eventList[ind] = obj;
	}
	setTimeout("GoOn(" + ind + ")", 1000);
}
function GoOn(ind) {
	var obj = window.eventList[ind];
	window.eventList[ind] = null;
	if (obj.NextStep)
		obj.NextStep();
	else
		obj();
}
//格式化日期时间 
function timeStamp2String(time){
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date+" "+hour+":"+minute+":"+second;
}
$(function(){
	ly = {};
	ly.ajax = (function(params) {
		var pp = {
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert('出错啦！');				
			}
		};
		$.extend(pp, params);
		$.ajax(pp);
	});
	ly.ajaxSubmit = (function(form, params) {// form 表单ID. params ajax参数
		var pp = {
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				alert('出错啦！');				
			}
		};
		$.extend(pp, params);
		$(form).ajaxSubmit(pp);
	});
	CommnUtil = {
			/**
			 * ajax同步请求 返回一个html内容 dataType=html. 默认为html格式 如果想返回json.
			 * CommnUtil.ajax(url, data,"json")
			 * 
			 */
			ajax : function(url, data, dataType) {
				if (!CommnUtil.notNull(dataType)) {
					dataType = "html";
				}
				var result = '没有结果!';
				// 所以AJAX都必须使用ly.ajax..这里经过再次封装,统一处理..同时继承ajax所有属性
				if (url.indexOf("?") > -1) {
					url = url + "&_t=" + new Date();
				} else {
					url = url + "?_t=" + new Date();
				}
				ly.ajax({
					type : "post",
					data : data,
					url : url,
					dataType : dataType,// 这里的dataType就是返回回来的数据格式了html,xml,json
					async : false,
					cache : false,// 设置是否缓存，默认设置成为true，当需要每次刷新都需要执行数据库操作的话，需要设置成为false
					success : function(data) { //data不能与result重名
						result = data;
					}
				});
				return result;
			},
			/**
			 * 判断某对象不为空..返回true 否则 false
			 */
			notNull : function(obj) {
				if (obj === null) {
					return false;
				} else if (obj === undefined) {
					return false;
				} else if (obj === "undefined") {
					return false;
				} else if (obj === "") {
					return false;
				} else if (obj === "[]") {
					return false;
				} else if (obj === "{}") {
					return false;
				} else {
					return true;
				}
	
			},
	
			/**
			 * 判断某对象不为空..返回obj 否则 ""
			 */
			notEmpty : function(obj) {
				if (obj === null) {
					return "";
				} else if (obj === undefined) {
					return "";
				} else if (obj === "undefined") {
					return "";
				} else if (obj === "") {
					return "";
				} else if (obj === "[]") {
					return "";
				} else if (obj === "{}") {
					return "";
				} else {
					return obj;
				}
	
			},
			loadingImg : function() {
				var html = '<div class="alert alert-warning">'
						+ '<button type="button" class="close" data-dismiss="alert">'
						+ '<i class="ace-icon fa fa-times"></i></button><div style="text-align:center">'
						+ '<img src="' + rootPath + '/images/loading.gif"/><div>'
						+ '</div>';
				return html;
			},
			/**
			 * html标签转义
			 */
			htmlspecialchars : function(str) {
				var s = "";
				if (str.length == 0)
					return "";
				for (var i = 0; i < str.length; i++) {
					switch (str.substr(i, 1)) {
					case "<":
						s += "&lt;";
						break;
					case ">":
						s += "&gt;";
						break;
					case "&":
						s += "&amp;";
						break;
					case " ":
						if (str.substr(i + 1, 1) == " ") {
							s += " &nbsp;";
							i++;
						} else
							s += " ";
						break;
					case "\"":
						s += "&quot;";
						break;
					case "\n":
						s += "";
						break;
					default:
						s += str.substr(i, 1);
						break;
					}
				}
			},
			/**
			 * in_array判断一个值是否在数组中
			 */
			in_array : function(array, string) {
				for (var s = 0; s < array.length; s++) {
					thisEntry = array[s].toString();
					if (thisEntry == string) {
						return true;
					}
				}
				return false;
			},
			uuid:function(){
			    function S4() {
			       return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
			    }
			    return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
			}
			
		};
});